package com.sap.jcp.sm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.sap.jcp.sm.domain.UserTimeline;

public class DateUtil {
	
	public static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
	
	public static String getWeekCode(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(calendar.DAY_OF_WEEK);
		int subVal = dayOfWeek;
		calendar.add(Calendar.DATE, (-1*subVal));
		
		String WeekId = sdf.format(calendar.getTime());
		//System.out.println(WeekId);
		return WeekId;
	}
	
	public static String getNextWeekId(String currentWeekId) {
		try {
			Date currentWeekDate = sdf.parse(currentWeekId);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(currentWeekDate);
			calendar.add(Calendar.DATE, 7);
			
			Date nextWeekDate = calendar.getTime();
			return sdf.format(nextWeekDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getPreviousWeekId(String currentWeekId) {
		try {
			Date currentWeekDate = sdf.parse(currentWeekId);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(currentWeekDate);
			calendar.add(Calendar.DATE, -7);
			
			Date previousWeekDate = calendar.getTime();
			return sdf.format(previousWeekDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getFormattedStringFromDate(Date date) {
		String formattedString = sdf.format(date);
		return formattedString;
	}
	
	public static List<String> getLastFiveWeekIds(Date date) {
		List<String> weekIdList = new ArrayList<String>();
		String currentWeekCode  = getWeekCode(date);
		String nextWeekId = getNextWeekId(currentWeekCode);
		weekIdList.add(nextWeekId);
		weekIdList.add(currentWeekCode);
		for (int i = 0 ; i < 3 ; i++) {
			String previousWeekCode = getPreviousWeekId(currentWeekCode);
			weekIdList.add(previousWeekCode);
			currentWeekCode = previousWeekCode;
		}
		return weekIdList;
		
	}
	
	public static List<String> getWeekDateList(String weekId) {
		List<String> weekDateList = new ArrayList<String>();
		try {
			weekDateList.add(weekId);
			
			for (int i = 0; i < 6 ; i++) {
				Date date = sdf.parse(weekId);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DATE, 1);
				Date nextDayDate = calendar.getTime();
				String nextDate = sdf.format(nextDayDate);
				weekDateList.add(nextDate);
				weekId = nextDate;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return weekDateList;
	}

	public static boolean isWeekEnd(String weekId) {
		Date date;
		try {
			date = sdf.parse(weekId);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			if (calendar.get(calendar.DAY_OF_WEEK) == calendar.SATURDAY || calendar.get(calendar.DAY_OF_WEEK) == calendar.SUNDAY) {
				return true;
			} 
			return false;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
}
